﻿using BunnyEater.Common.Jwt.Dto;
using BunnyEater.Common.ResponseOutput;
using BunnyEater.Domain.Base;
using BunnyEater.Domain.SystemManagement;
using BunnyEater.Service.SystemManagementService.UserService;
using BunnyEater.Service.SystemManagementService.UserService.Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BunnyEater.WebAPI.Controllers.SysManage
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "SYSTEM")]
    public class UsersController : ControllerBase
    {
        private readonly IUsersService _usersService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        /// <summary>
        /// 注入
        /// </summary>
        public UsersController(IUsersService usersService, IHttpContextAccessor httpContextAccessor)
        {
            _usersService = usersService;
            _httpContextAccessor = httpContextAccessor;
        }
        #region 登陆

        /// <summary>
        /// 用户登录
        /// </summary>
        [HttpPost]
        [AllowAnonymous]
        [ApiExplorerSettings(GroupName = "LOGIN")]
        public ResponseReturnsEntity<LoginRequest> Login(LoginRequest loginRequest)
        {
            var result = _usersService.Login(loginRequest);
            return result.Result;
        }


        #endregion


        #region 查询

        /// <summary>
        /// 主键获取
        /// </summary>
        [HttpGet]
        public ResponseReturnsEntity<User> GetUserById(string id)
        {
            var result = _usersService.GetUserById(id);
            return result.Result;
        }

        /// <summary>
        /// 获取用户详情（包含角色信息）
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <returns>用户详情（包含角色列表）</returns>
        [HttpGet]
        public async Task<ResponseReturnsEntity<UserDetailDto>> GetUserDetailById(string id)
        {
            var result = await _usersService.GetUserDetailById(id);
            return result;
        }

        /// <summary>
        /// 分页查询用户列表
        /// </summary>
        /// <param name="query">分页查询参数</param>
        /// <returns>分页用户列表</returns>
        [HttpGet]
        [AllowAnonymous]//匿名访问
        public async Task<ResponseReturnsEntity<Page<UserListDto>>> GetPagedUsers([FromQuery] UserPagedQueryDto query)
        {
            var result = await _usersService.GetPagedUsersAsync(query);
            return result;
        }

        /// <summary>
        /// 五表联查：获取用户及其角色和权限信息
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>用户角色权限信息</returns>
        [HttpGet]
        public async Task<ResponseReturnsEntity<UserRolePermissionDto>> GetUserRolePermissions(string userId)
        {
            try
            {
                var result = await _usersService.GetUserRolePermissions(userId);
                return ResponseReturnsEntity<UserRolePermissionDto>.Success(result);
            }
            catch (InvalidOperationException ex)
            {
                return ResponseReturnsEntity<UserRolePermissionDto>.Fail(ex.Message);
            }
            catch (Exception ex)
            {
                return ResponseReturnsEntity<UserRolePermissionDto>.Fail($"获取用户角色权限信息失败: {ex.Message}");
            }
        }

        #endregion



        #region 业务

        /// <summary>
        /// 添加用户
        /// </summary>
        //[AllowAnonymous]//匿名访问
        [HttpPost]
        public ResponseReturnsEntity<bool> AddUser(UserRequest userRequest)
        {
            var result = _usersService.AddUser(userRequest);
            return result.Result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        [HttpPost]
        public ResponseReturnsEntity<bool> UptUser(UserRequest userRequest)
        {
            var result = _usersService.UptUser(userRequest);
            return result.Result;
        }
        /// <summary>
        /// 修改状态(启用禁用)
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseReturnsEntity<bool> UpdateUserState(UserRequest userDto)
        {
            var result = _usersService.UpdateUserState(userDto);
            return result.Result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseReturnsEntity<bool> DeleteUser(UserDto userDto)
        {
            var result = _usersService.DeleteUser(userDto);
            return result.Result;
        }



        #endregion






    }
}
